package lnu.project.cart;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class AddNewProduct extends Activity {
	private Activity cActivity;
	private static int TYPE = 0;
	private static String BARCODE = "112288";

	private EditText editname, editprice, editunit, barcodetext;
	private Spinner spinner;
	private Button save_button, barcode_button;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newproduct_xml);
		cActivity = this;

		barcode_button = (Button) findViewById(R.id.BTbarcode);
		barcode_button.setOnClickListener(new BarcodeButtonClick());

		save_button = (Button) findViewById(R.id.save);
		save_button.setOnClickListener(new ButtonClick());

		spinner = (Spinner) findViewById(R.id.product_type);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.type_array, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		spinner.setOnItemSelectedListener(new TypeOnItemSelectedListener());

		editname = (EditText) findViewById(R.id.proname);
		editunit = (EditText) findViewById(R.id.unit);
		editprice = (EditText) findViewById(R.id.pricetext);
		barcodetext = (EditText) findViewById(R.id.barcodeText);
	}

	public class TypeOnItemSelectedListener implements OnItemSelectedListener {
		public void onItemSelected(AdapterView<?> parent, View view, int pos,
				long id) {
			TYPE = pos;
			TextView textUnit = (TextView) findViewById(R.id.textunit);
			switch (pos) {
			case 0:
				textUnit.setText(R.string.quantity);
				break;
			case 1:
				textUnit.setText(R.string.weight);
				break;
			default:
				break;
			}

			Toast.makeText(
					parent.getContext(),
					"The planet is " + parent.getItemAtPosition(pos).toString(),
					Toast.LENGTH_LONG).show();
		}

		public void onNothingSelected(AdapterView<?> parent) {
		}
	}

	private class ButtonClick implements View.OnClickListener {
		public void onClick(View v) {
			String name = editname.getText().toString();
			String unit = editunit.getText().toString();
			String price = editprice.getText().toString();
			String barcode = barcodetext.getText().toString();

			/* Create new result intent */
			Intent reply = new Intent();
			reply.putExtra("name", name);
			reply.putExtra("productType", TYPE);
			// If state
			if (TYPE == 0) {
				reply.putExtra("quantity", Integer.parseInt(unit));
				reply.putExtra("weight", Float.parseFloat("0"));
			} else if (TYPE == 1) {
				reply.putExtra("quantity", 0);
				reply.putExtra("weight", Float.parseFloat(unit));
			}

			reply.putExtra("price", Float.parseFloat(price));
			reply.putExtra("barcode", barcode);

			reply.putExtra("flag", "0");
			setResult(RESULT_OK, reply);
			finish(); // Close this activity
		}
	}

	private class BarcodeButtonClick implements View.OnClickListener {
		public void onClick(View v) {
			String packageName = "com.google.zxing.client.android";
			Uri marketUri = Uri.parse("market://details?id=" + packageName);
			try {
				Intent intent = new Intent(
						"com.google.zxing.client.android.SCAN");
				intent.setPackage(packageName);
				intent.putExtra("SCAN_MODE", "SCAN_MODE");
				startActivityForResult(intent, 0);
			} catch (Exception e) {
				final Intent marketIntent = new Intent(
						android.content.Intent.ACTION_VIEW, marketUri);
				startActivity(marketIntent);
			}
		}
	}

	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		if (requestCode == 0) {
			if (resultCode == RESULT_OK) {
				String contents = intent.getStringExtra("SCAN_RESULT");
				String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
				barcodetext.setText(contents);
			} else if (resultCode == RESULT_CANCELED) {
				Toast.makeText(cActivity, "Barcode reader cancelled",
						Toast.LENGTH_LONG);
			}
		}
	}
}
